package sf.database.jdbc.lob;

import sf.tools.IOUtils;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.io.Reader;
import java.io.Serializable;
import java.io.Writer;
import java.sql.Clob;
import java.sql.SQLException;

public class SimpleClob implements Clob, Serializable {

    private static final long serialVersionUID = -5465130240603178708L;

    private File file;

    public SimpleClob() {
    }

    public SimpleClob(File f) {
        this.file = f;
    }

    public long length() throws SQLException {
        return file.length();
    }

    public String getSubString(long pos, int length) throws SQLException {
        if (pos < 1)
            throw new SQLException("pos<1");
        pos--;
        String str = null;
        try {
            str = IOUtils.asString(file, "UTF-8");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        if (pos >= length)
            throw new IllegalArgumentException("pos=" + pos);
        if (pos + length >= length())
            return str.substring((int) pos);
        return str.substring((int) pos, (int) (pos + length - 1));
    }

    public Reader getCharacterStream() throws SQLException {
        try {
            return IOUtils.getReader(file, "UTF-8");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public InputStream getAsciiStream() throws SQLException {
        try {
            return new BufferedInputStream(new FileInputStream(file));
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    public long position(String searchstr, long start) throws SQLException {
        throw new UnsupportedOperationException();
    }

    public long position(Clob searchstr, long start) throws SQLException {
        throw new UnsupportedOperationException();
    }

    public int setString(long pos, String str) throws SQLException {
        throw new UnsupportedOperationException();
    }

    public int setString(long pos, String str, int offset, int len) throws SQLException {
        throw new UnsupportedOperationException();
    }

    public OutputStream setAsciiStream(long pos) throws SQLException {
        throw new UnsupportedOperationException();
    }

    public Writer setCharacterStream(long pos) throws SQLException {
        throw new UnsupportedOperationException();
    }

    public void truncate(long len) throws SQLException {
        try {
            RandomAccessFile raf = new RandomAccessFile(file, "rw");
            raf.setLength(len);
            raf.close();
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public void free() throws SQLException {
        if (file.exists()) {
            file.delete();
        }
    }

    public Reader getCharacterStream(long pos, long length) throws SQLException {
        throw new UnsupportedOperationException();
    }

}
